package enum

type ErrorCode int

const (
	SUCCESS     = 200
	SYSTEM_ERR  = 400
	PARAM_ERR   = 401
	IS_LOCK_ERR = 402

	DATA_NOT_EXIST_CODE      = 501
	DATA_CAT_NOT_UPDATE_CODE = 502
	DATA_EXPIRED             = 503
	LOGIN_ERROR              = 504

	TOKEN_INVALID = 505
	TOKEN_EXPIRED = 506

	REPEAT_SUBMIT_ANSWER = 507
)

var (
	OK *ErrorObj = &ErrorObj{Code: SUCCESS, Msg: "success"}

	SystemErr  *ErrorObj = &ErrorObj{Code: SYSTEM_ERR, Msg: "系统异常"}
	RequestErr *ErrorObj = &ErrorObj{Code: SYSTEM_ERR, Msg: "请求异常"}
	ParamErr   *ErrorObj = &ErrorObj{Code: PARAM_ERR, Msg: "param error"}
	LockErr    *ErrorObj = &ErrorObj{Code: IS_LOCK_ERR, Msg: "lock error"}

	DataNotExistErr  *ErrorObj = &ErrorObj{Code: DATA_NOT_EXIST_CODE, Msg: "数据不存在"}
	DataCanNotUpdate *ErrorObj = &ErrorObj{Code: DATA_CAT_NOT_UPDATE_CODE, Msg: "数据不能修改"}
	DataExpired      *ErrorObj = &ErrorObj{Code: DATA_EXPIRED, Msg: "过期了"}
	LoginErr         *ErrorObj = &ErrorObj{Code: LOGIN_ERROR, Msg: "登录异常"}
	UserNotExistErr  *ErrorObj = &ErrorObj{Code: LOGIN_ERROR, Msg: "用户不存在"}

	// token
	TokenInvalid *ErrorObj = &ErrorObj{Code: TOKEN_INVALID, Msg: "token invalid"}
	TokenExpired *ErrorObj = &ErrorObj{Code: TOKEN_EXPIRED, Msg: "token expired"}

	RepeatSubmitAnswerErr *ErrorObj = &ErrorObj{Code: REPEAT_SUBMIT_ANSWER, Msg: "repeat submit answer"}
)

type ErrorObj struct {
	Code ErrorCode
	Msg  string
}

func (err ErrorObj) Error() string {
	return err.Msg
}

func NewErrorObj(err error) *ErrorObj {
	return &ErrorObj{
		Code: SYSTEM_ERR,
		Msg:  err.Error(),
	}
}
